/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import vn.webapp.model.Sessions;

@Repository("sessionsDAO")
//@SuppressWarnings({"unchecked", "rawtypes"})
@Transactional
public class SessionsDAOImpl extends BaseDao implements SessionsDAO {

    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public List<Sessions> list() {
        try {
            begin();
            Criteria criteria = getSession().createCriteria(Sessions.class);
            //criteria.setMaxResults(10);
            //criteria.setFirstResult(0);
            List<Sessions> sessions = criteria.list();
            commit();
            return sessions;
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return null;
        } finally {
            flush();
            close();
        }
    }
    
    @Override
    public Sessions viewDetail(String id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Sessions c WHERE c.session_id = '" + id + "'");
        return (Sessions) q.uniqueResult();
    }
    
    @Override
    public Sessions checkExistsYear(String year, int semester, int id) {
        Query q = sessionFactory.getCurrentSession().createQuery(
                "FROM Sessions c WHERE c.year = '" + year + "' AND c.semester = " + semester + " AND c.session_id != '" + id + "'");
        return (Sessions) q.uniqueResult();
    }
    
    @Override
    public int updateTheSession(String year, int semester, int id){
        Query query = sessionFactory.getCurrentSession().createQuery(
                "UPDATE Sessions SET year ='" + year + "', semester = '" + semester + "' WHERE session_id = '" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int removeSession(int id) {
        Query query = sessionFactory.getCurrentSession().createQuery(
                "DELETE from Sessions r WHERE r.session_id ='" + id + "'");
        int result = query.executeUpdate();
        return result;
    }
    
    @Override
    public int saveASession(Sessions session)
    {
        try {
           begin();
           int id = 0; 
           id = (int)getSession().save(session);
           commit();
           return id;           
        } catch (HibernateException e) {
            e.printStackTrace();
            rollback();
            close();
            return 0;
        } finally {
            flush();
            close();
        }
    }
}
